/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.edu.ifrn.dao;

import br.edu.ifrn.dominio.Administrador;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
/**
 *
 * @author Braga
 */
public class AdministradorDAO {
    public AdministradorDAO(){

    }

    public void insertAdmininistrador (Administrador a) throws SQLException {
        Connection conn = ConnectionFactory.getConnection();

        String sql = "INSERT INTO ADMINISTRADOR VALUES (?,?, ?)";

        PreparedStatement stm = conn.prepareStatement (sql);

        stm.setInt(1, a.getID ());
        stm.setString(2, a.getSenha());
        stm.setString(3, a.getPesquisador_CPF());

        stm.executeUpdate();

    }

    public void deleteAdministrador(int ID) throws SQLException{
        Connection conn = ConnectionFactory.getConnection();
        
        String sql = "DELETE FROM ADMINISTRADOR WHERE ID = ?";
        
        PreparedStatement stm = conn.prepareStatement (sql);
        
        stm.setInt(1, ID);
        
        stm.executeUpdate ();
    }

    public void updateAdministrador(int ID, Administrador a) throws SQLException{
        Connection conn = ConnectionFactory.getConnection();

        String sql = "UPDATE ADMINISTRADOR SET SENHA = ?, PESQUISADOR_CPF = ? WHERE ID = ?";

        PreparedStatement stm = conn.prepareStatement (sql);

        stm.setString(1, a.getSenha());
        stm.setString(2, a.getPesquisador_CPF());
        stm.setInt(3, ID);

        stm.executeUpdate ();
    }

    public LinkedList selectAdministrador() throws SQLException{
        Connection conn = ConnectionFactory.getConnection();
        LinkedList list = new LinkedList();
        ResultSet rs = null;

        String sql = "SELECT * FROM ADMINISTRADOR";

        PreparedStatement stm = conn.prepareStatement(sql);

        rs = stm.executeQuery();

        while(rs.next()){
            Administrador a = new Administrador();
            
            a.setID(rs.getInt(1));
            a.setSenha(rs.getString(2));
            a.setPesquisador_CPF(rs.getString(3));

            list.add(a);
        }

        return list;
    }

    public LinkedList selectAdministradorById(int ID) throws SQLException{
        Connection conn = ConnectionFactory.getConnection();
        LinkedList list = new LinkedList();
        ResultSet rs = null;

        String sql = "SELECT * FROM ADMINISTRADOR WHERE ID = ?";

        PreparedStatement stm = conn.prepareStatement(sql);

        stm.setInt(1, ID);

        rs = stm.executeQuery();

        while (rs.next()){
            Administrador a = new Administrador();

            a.setID(rs.getInt(1));
            a.setSenha(rs.getString(2));
            a.setPesquisador_CPF(rs.getString(3));

            list.add(a);
        }

        return list;
        }
}